<?php

if (!defined('IN_XLP')) {
    exit('Access Denied!');
}

/**
 * Description of sechandAction
 *
 * @author xlp
 */
class sechandAction extends commonAction {

    private $cacheFilePre = 'sechand/visit_vid_'; //保存缓存信息的文件名前缀
    private $info = array('否', '<span class="red">是</span>');
    private $status = array('<span class="red">屏蔽</span>', '正常');
    private $type = array('卖', '买');
    private $topicScreen = array(0 => '未填写', 5 => '五成新', 6 => '六成新', 7 => '七成新', 8 => '八成新', 9 => '九成新', 10 => '全新');

    function __construct() {
        parent::__construct();
        parent::_checkLogin();
    }

    function index() {
        $title = $this->_get('q');
        $stime = $this->_get('stime');
        $etime = $this->_get('etime');
        $p = $this->_getid('p', 1);
        $uid = $this->_getid('uid', 0);
        $village_id = $this->_getid('village_id', 0);
        $type = $this->_getid('type', 0);
        $hot = $this->_getid('hot', 0);
        $top = $this->_getid('top', 0);
        $report = $this->_getid('report', 0);
        $status = $this->_getid('status', 0);
        $recycle = false; //是否回收站
        $where = array();
        if ($title) {
            $where['LIKE'] = array('title' => parent::safeSearch($title));
        }
        if ($stime && $etime) {
            $stime = inTime($stime);
            $etime = inTime($etime) + (24 * 60 * 60) - 1;
        }
        if ($stime && $etime) {
            $where['infotime[>=]'] = $stime;
            $where['infotime[<=]'] = $etime;
        }
        if ($uid) {
            $where = array('uid' => $uid);
        }
        if ($village_id) {
            $where = array('village_id' => $village_id);
        }
        if ($type) {
            $where = array('type' => ($type - 1));
        }
        if ($hot) {
            $where = array('is_hot' => $hot);
        }
        if ($top) {
            $where = array('is_top' => $top);
        }
        if ($report) {
            $where = array('is_report' => $report);
        }
        if ($status) {
            $where = array('status' => 0);
            $recycle = true;
        } else {
            $where['status'] = 1;
        }
        $rs = M('sechand_trade')->where($where)->page($p)->order('sid DESC')->findAll();
        $total = M('sechand_trade')->getTotal();
//        z(M('area')->getAllSql());
        if ($rs) {
            foreach ($rs as $k => $v) {
                $rs[$k]['user_name'] = getUser($v['uid']);
                $rs[$k]['village_name'] = parent::getVillageName($v['village_id']);
            }
        }
        $this->assign(array('rs' => $rs, 'total' => $total, 'p' => $p, 'info' => $this->info, 'status' => $this->status, 'type' => $this->type, 'recycle' => $recycle));
//        print_r($this->getAllVar());
        $this->display();
    }

    function detail() {
        $sid = $this->_getid('sid');
        if (!$sid) {
            showError('参数丢失');
        }
        $rs = M('sechand_trade')->where(array('sid' => $sid))->find();
        if (!$rs) {
            showError('主题不存在或者已被删除');
        }
        $rs['content'] = express($rs['content']);
        $rs['lbs_info'] = json_decode($rs['lbs_info'], true);
        $rs['fineness'] = isset($this->topicScreen[$rs['fineness']]) ? $this->topicScreen[$rs['fineness']] : '';
        $rs['pics_list'] = $rs['photo_counts'] ? D('album')->getPhotosById($rs['uid'], $rs['pics_list']) : array();
        $this->assign(array('rs' => $rs));
//        z($rs);
        $this->display();
    }

    function setting() {
        $id = $this->_postid('id');
        $isHot = $this->_postid('is_hot', 0);
        $isTop = $this->_postid('is_top', 0);
        $isOk = $this->_postid('is_ok', 0);
        $where = array();
        if (!$id) {
            showError('参数丢失');
        }
        if ($isHot) {
            $where = $isHot == 1 ? array('is_hot' => 1) : array('is_hot' => 0);
        }
        if ($isTop) {
            $where = $isTop == 1 ? array('is_top' => 1) : array('is_top' => 0);
        }
        if ($isOk) {
            $where = array('status' => 1);
        }
        if ($where) {
            M('sechand_trade')->update($where, array('sid' => $id));
            $this->JsonReturn('ok', null, 1);
        } else {
            $this->JsonReturn('error');
        }
    }

    function delete() {
        $id = $this->_postid('id');
        if (!$id) {
            showError('参数丢失');
        }
        //检查主题是否存在
        $sechand = M('sechand_trade')->field('uid,village_id')->where(array('sid' => $id))->find();
        if (!$sechand) {
            $this->JsonReturn('主题不存在，请检查');
        }
        M('sechand_trade')->update(array('status' => 0), array('sid' => $id));
        F($this->cacheFilePre . $sechand['village_id'], null); //清除统计缓存
        $this->JsonReturn('ok', null, 1);
    }

    function clear() {
        $id = $this->_postid('id');
        if (!$id) {
            showError('参数丢失');
        }
        //检查主题是否存在
        $sechand = M('sechand_trade')->field('uid,village_id')->where(array('sid' => $id))->find();
        if (!$sechand) {
            $this->JsonReturn('主题不存在，请检查');
        }
        M('sechand_trade')->delete(array('sid' => $id));
        //删除所有回帖
        M('scenery_post')->delete(array('sid' => $id));
        M('scenery_praise')->delete(array('sid' => $id));
        F($this->cacheFilePre . $sechand['village_id'], null); //清除统计缓存
        $this->JsonReturn('ok', null, 1);
    }

}
